package main

import (
	"fmt"
	"github.com/myMonitor/funcs"
	"github.com/toolkits/core"
	"github.com/toolkits/nux"
	"log"
	"runtime"
	"time"
)

func main() {
	fmt.Println("----------以下是CPU信息-----------\n")
	fmt.Printf("cpu核数 %d \n",runtime.NumCPU())//CPU 核数
	for i := 0; i < 3; i++ {
		funcs.UpdateCpuStat()
		fmt.Printf("第%d次 cpu空闲为:%f  cpu已使用%f \n",i, funcs.CpuIdle(),100 - funcs.CpuIdle())
		//fmt.Printf("CpuInfo : %v\n",funcs.CpuMetrics())
		time.Sleep(time.Second * 1)
	}

	fmt.Println("----------以下是磁盘信息-----------\n")
	mountPoints, err := nux.ListMountPoint()
	var ret [][]interface{} = make([][]interface{}, 0)
	for idx := range mountPoints {
		var du *nux.DeviceUsage
		du, err = nux.BuildDeviceUsage(mountPoints[idx][0], mountPoints[idx][1], mountPoints[idx][2])
		if err == nil {
			ret = append(ret,
				[]interface{}{
					du.FsSpec,
					core.ReadableSize(float64(du.BlocksAll)),
					core.ReadableSize(float64(du.BlocksUsed)),
					core.ReadableSize(float64(du.BlocksFree)),
					fmt.Sprintf("%.1f%%", du.BlocksUsedPercent),
					du.FsFile,
					core.ReadableSize(float64(du.InodesAll)),
					core.ReadableSize(float64(du.InodesUsed)),
					core.ReadableSize(float64(du.InodesFree)),
					fmt.Sprintf("%.1f%%", du.InodesUsedPercent),
					du.FsVfstype,
				})
		}
	}
	fmt.Println(ret)
	fmt.Println("----------以下是磁盘信息二-----------\n")
	diskList := funcs.DiskInfo()
	fmt.Println(diskList)



	fmt.Println("----------以下是内存信息-----------\n")
	mem, err := nux.MemInfo()
	memFree := mem.MemFree + mem.Buffers + mem.Cached
	memUsed := mem.MemTotal - memFree
	var t uint64 = 1024 * 1024
	fmt.Printf("total ： %dM \n",mem.MemTotal / t)
	fmt.Printf("free ： %dM \n",memFree / t)
	fmt.Printf("used ： %dM \n",memUsed / t)

	fmt.Println("----------以下是网络信息-----------\n")

	fmt.Println("验证网络名称list函数 NetworkNameList")
	name := funcs.NetworkNameList()
	fmt.Println(name)

	fmt.Println("验证网络信息函数")
	fmt.Println(funcs.NetWorkInfo(name[0]))

	fmt.Println("验证网速。。。")
	fmt.Println(funcs.NetworkSpeed(name[0]))
	fmt.Println(funcs.NetworkSpeed(name[0]))
	fmt.Println(funcs.NetworkSpeed(name[0]))
	fmt.Println(funcs.NetworkSpeed(name[0]))


	var oldnet *nux.NetIf
	for i := 0; i < 100; i++ {
		var ifacePrefix = []string{"eth", "em", "ens"}
		netIfs, err := nux.NetIfs(ifacePrefix)
		if err != nil {
			log.Println(err)
			fmt.Printf("网络异常\n")
			return
		}
		if i == 0 {
			oldnet = netIfs[0];
		}
		for index,data := range netIfs {
			if i == 0 {
				break
			}
			fmt.Printf("第 %d 块网卡信息 \n",index+1)
			fmt.Printf("Iface(网卡名) : %s\n",data.Iface)
			fmt.Printf("InBytes(已接收总字节数) : %d \n",data.InBytes)
			fmt.Printf("InPackages(已接收总包数) : %d \n",data.InPackages)
			//fmt.Printf("InErrors : %d \n",data.InErrors)
			//fmt.Printf("InDropped : %d \n",data.InDropped)
			//fmt.Printf("InFifoErrs : %d \n",data.InFifoErrs)
			//fmt.Printf("InFrameErrs : %d \n",data.InFrameErrs)
			//fmt.Printf("InCompressed : %d \n",data.InCompressed)
			//fmt.Printf("InMulticast : %d \n",data.InMulticast)
			fmt.Printf("OutBytes(已上传总字节数) : %d \n",data.OutBytes)
			fmt.Printf("OutPackages(已上传总包数) : %d \n",data.OutPackages)
			//fmt.Printf("OutErrors : %d \n",data.OutErrors)
			//fmt.Printf("OutDropped : %d \n",data.OutDropped)
			//fmt.Printf("OutFifoErrs : %d \n",data.OutFifoErrs)
			//fmt.Printf("OutCollisions : %d \n",data.OutCollisions)
			//fmt.Printf("OutCompressed : %d \n",data.OutCompressed)
			//fmt.Printf("OutCarrierErrs : %d \n",data.OutCarrierErrs)
			fmt.Printf("TotalBytes(总字节数) : %d \n",data.TotalBytes)
			fmt.Printf("TotalPackages(总包数) : %d \n",data.TotalPackages)
			//fmt.Printf("TotalErrors : %d \n",data.TotalErrors)
			//fmt.Printf("TotalDropped : %d \n",data.TotalDropped)
			//fmt.Printf("SpeedBits : %d \n",data.SpeedBits)
			//fmt.Printf("InPercent : %f \n",data.InPercent)
			//fmt.Printf("OutPercent : %f \n",data.OutPercent)
			fmt.Printf("下载网速为 : %f kb/s\n",float64((data.InBytes - oldnet.InBytes))/1024)
			fmt.Printf("上传网速为 : %f kb/s\n",float64((data.OutBytes - oldnet.OutBytes))/1024)
		}
		oldnet = netIfs[0];
		time.Sleep(time.Second * 1)
	}



}